package com.grupoconstruobras.sigo.exhumaciones.ui.validator;

import java.io.Serializable;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.servlet.http.Part;

@FacesValidator(value="FileValidator")
public class FileValidator implements Validator, Serializable{

	private ResourceBundle bundle;
	
	public FileValidator() {
		FacesContext fc = FacesContext.getCurrentInstance();
		bundle = fc.getApplication().evaluateExpressionGet(fc, "#{msgVal}", ResourceBundle.class);
	}
	
	@Override
	public void validate(FacesContext fc, UIComponent component, Object value)
			throws ValidatorException {
		
		Part fileToUpload = (Part)value;
		
		if(fileToUpload == null){
			return;
		}
		
		if(fileToUpload.getSize() >= 10485760){
			String msg = bundle.getString("constraint.date.nofuturedate");
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg +"la actual", null);
			fc.addMessage(component.getClientId(), message);
			throw new ValidatorException(message);
		}
		
	}

}
